package com.weifly.jettycn.control;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 抽象logic
 * 
 * @author weijx
 * @date 2011-10-06
 */
public class AbstractLogic {

	/**
	 * 写回ajax数据
	 * 
	 * @param data 字符串内容
	 */
	protected void writeAjaxString(HttpServletResponse response, String data) {
		if (data == null) {
			return;
		}
		response.setContentType("text/html;charset=utf-8");
		try {
			response.getWriter().print(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 写回ajax数据
	 * 
	 * @param data list或map对象
	 */
	protected void writeAjaxObject(HttpServletResponse response, Object data){
		if(data == null){
			return;
		}
		String str = null;
		if((data instanceof Collection<?>) || (data.getClass().isArray())){
			str = JSONArray.fromObject(data).toString();
		}else{
			str = JSONObject.fromObject(data).toString();
		}
		writeAjaxString(response, str);
	}
}
